let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isBoolean,
    isArray
} = require('./dataType')

const goods = class goods {

    constructor() {
    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if (!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name;
        }
        if (!isUndefined(options.categoryId) && isString(options.categoryId)) {
            params.categoryId = options.categoryId;
        }
        if (!isUndefined(options.subCategoryId) && isString(options.subCategoryId)) {
            params.subCategoryId = options.subCategoryId;
        }
        if (!isUndefined(options.sellerId) && isString(options.sellerId)) {
            params.sellerId = options.sellerId;
        }
        if (!isUndefined(options.status) && isBoolean(options.status)) {
            params.status = options.status;
        }
        if (!isUndefined(options.hotSale) && isBoolean(options.hotSale)) {
            params.hotSale = options.hotSale;
        }
        if (!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        if (!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.LOAD_GOODS.url,
                MS._config.parameters.SHOP.LOAD_GOODS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    get(id) {
        let data = {goodsId: id}
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.GET_GOODS.url,
                MS._config.parameters.SHOP.GET_GOODS.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    loadGoodsCategorys() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.LOAD_GOODS_CATEGORY.url,
                MS._config.parameters.SHOP.LOAD_GOODS_CATEGORY.method, {}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    loadGoodsSubCategorys() {
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.LOAD_SUB_GOODS_CATEGORYS.url,
                MS._config.parameters.SHOP.LOAD_SUB_GOODS_CATEGORYS.method, {}).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }

    favorite(goodsId) {
        if (!MS.User.current()) {
            throw new Error(416, "当前用户未登录")
        }
        let data = {
            userId: MS.User.current().id,
            goodsId: goodsId,
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FAVORITE.url,
                MS._config.parameters.SHOP.FAVORITE.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    cancelFavorite(goodsId) {
        if (!MS.User.current()) {
            throw new Error(416, "当前用户未登录")
        }
        let params = {
            userId: MS.User.current().id,
            goodsId: goodsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.CANCEL_FAVORITE.url,
                MS._config.parameters.SHOP.CANCEL_FAVORITE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    checkFavorite(goodsId) {
        if (!MS.User.current()) {
            throw new Error(416, "当前用户未登录")
        }
        let params = {
            userId: MS.User.current().id,
            goodsId: goodsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.CHECK_FAVORITE.url,
                MS._config.parameters.SHOP.CHECK_FAVORITE.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    loadUserFavoriteGoods(pageNum, pageSize) {
        if (!MS.User.current()) {
            throw new Error(416, "当前用户未登录")
        }
        let params = {
            userId: MS.User.current().id,
            pageNum: pageNum != undefined || pageNum != null ? pageNum : 1,
            pageSize: pageSize != undefined || pageSize != null ? pageNum : 10
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.LOAD_USER_FAVORITE_GOODS.url,
                MS._config.parameters.SHOP.LOAD_USER_FAVORITE_GOODS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    save(options) {
        let params = {}
        if (!isUndefined(options.id) && isString(options.id)) {
            params.id = options.id
        }
        if (!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name
        }
        if (!isUndefined(options.remark) && isString(options.remark)) {
            params.remark = options.remark
        }
        if (!isUndefined(options.factory) && isString(options.factory)) {
            params.factory = options.factory
        }
        if (!isUndefined(options.categoryId) && isString(options.categoryId)) {
            params.categoryId = options.categoryId
        }
        if (!isUndefined(options.categoryName) && isString(options.categoryName)) {
            params.categoryName = options.categoryName
        }
        if (!isUndefined(options.subCategoryId) && isString(options.subCategoryId)) {
            params.subCategoryId = options.subCategoryId
        }
        if (!isUndefined(options.subCategoryName) && isString(options.subCategoryName)) {
            params.subCategoryName = options.subCategoryName
        }
        if (!isUndefined(options.sellerId) && isString(options.sellerId)) {
            params.sellerId = options.sellerId
        }
        if (!isUndefined(options.sellerName) && isString(options.sellerName)) {
            params.sellerName = options.sellerName
        }
        if (!isUndefined(options.status) && isBoolean(options.status)) {
            params.status = options.status
        }
        if (!isUndefined(options.hotSale) && isBoolean(options.hotSale)) {
            params.hotSale = options.hotSale
        }
        if (!isUndefined(options.ice) && isBoolean(options.ice)) {
            params.ice = options.ice
        }
        if (!isUndefined(options.isPublic) && isBoolean(options.isPublic)) {
            params.isPublic = options.isPublic
        }
        if (!isUndefined(options.images) && isArray(options.images)) {
            params.images = options.images
        }
        if (!isUndefined(options.video) && isArray(options.video)) {
            params.video = options.video
        }
        if (!isUndefined(options.logoShow) && isString(options.logoShow)) {
            params.logoShow = options.logoShow
        }
        if (!isUndefined(options.items) && isArray(options.items)) {
            params.items = options.items
            for (var i in params.items) {
                params.items[i].price = parseFloat(params.items[i].price) * 100
                params.items[i].shareProfit = parseFloat(params.items[i].shareProfit) * 100
                params.items[i].cost = parseFloat(params.items[i].cost) * 100
                params.items[i].primeCost = parseFloat(params.items[i].primeCost) * 100
            }
        }
        if (!isUndefined(options.goodsParams) && isArray(options.goodsParams)) {
            params.goodsParams = options.goodsParams
        }
        if (!isUndefined(options.removeItemIds) && isArray(options.removeItemIds)) {
            params.removeItemIds = options.removeItemIds
        }
        if (!isUndefined(options.productModel) && isString(options.productModel)) {
            params.productModel = options.productModel
        }
        if (!isUndefined(options.tagIds) && isArray(options.tagIds)) {
            params.tagIds = options.tagIds
        }
        if (!isUndefined(options.picManual) && isBoolean(options.picManual)) {
            params.picManual = options.picManual;
        }
        if (!isUndefined(options.picManualUrl) && isString(options.picManualUrl)) {
            params.picManualUrl = options.picManualUrl;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.SAVE_GOODS.url,
                MS._config.parameters.SHOP.SAVE_GOODS.method, {}, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    remove(goodsId) {
        let params = {
            goodsId: goodsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.REMOVE_GOODS.url,
                MS._config.parameters.SHOP.REMOVE_GOODS.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    updateStatus(goodsId) {
        let params = {
            goodsId: goodsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.UPDATE_GOODS_STATUS.url,
                MS._config.parameters.SHOP.UPDATE_GOODS_STATUS.method, params).then(res => {
                if (res.success) {
                    resolve(res.data)
                } else {
                    reject(res.message)
                }
            }).catch(err => {
                reject(err)
            })
        })
    }

    statisticsQuery(options) {
        let params = {}
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.goodsId) && isString(options.goodsId)) {
                params.goodsId = options.goodsId;
            }
            if(!isUndefined(options.sellerId) && isString(options.sellerId)) {
                params.sellerId = options.sellerId;
            }
            if(!isUndefined(options.appid) && isString(options.appid)) {
                params.appid = options.appid;
            }
            if(!isUndefined(options.startDate) && isString(options.startDate)) {
                params.startDate = options.startDate;
            }
            if(!isUndefined(options.endDate) && isString(options.endDate)) {
                params.endDate = options.endDate;
            }
            return new Promise((resolve ,reject) => {
                request(MS._config.parameters.SHOP.GOODS_STATISTICS_QUERY.url,
                    MS._config.parameters.SHOP.GOODS_STATISTICS_QUERY.method, params).then(res => {
                    if (res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
                }).catch(err => {
                    reject(err);
                })
            })
        }
    }

    findUserScanGoods(options) {
        let params = {
            userId: MS.User.current() ? MS.User.current().id : '',
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options) && isObject(options)) {
            if(!isUndefined(options.userId) && isString(options.userId)) {
                params.userId = options.userId;
            }
            if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
                params.pageNum = options.pageNum;
            }
            if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
                params.pageSize = options.pageSize;
            }
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.SHOP.FIND_USER_SCAN_GOODS.url,
                MS._config.parameters.SHOP.FIND_USER_SCAN_GOODS.method, params).then(res => {
                    if(res.success) {
                        resolve(res.data)
                    } else {
                        reject(res.message)
                    }
            }).catch(err => {
                reject(err)
            })
        })
    }
}
module.exports = goods
